有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java清除SharedReference中的单个变量

我的应用程序中保存了以下SharedPreference

editor.putString("strInfName", nameOfInf.getText().toString());
editor.putFloat("dblTollAmount", Float.parseFloat(tollAmount.getText().toString()));

如何只清除一个变量而不是两个

editor.clear();将清除所有变量,但我只想清除,strInfName而已

这是否有效:

editor.edit().remove("strInfName").commit();

共 (1) 个答案

  1. # 1 楼答案

    docs开始:

    public abstract SharedPreferences.Editor remove (String key)

    Added in API level 1 Mark in the editor that a preference value should be removed, which will be done in the actual preferences once commit() is called.

    Note that when committing back to the preferences, all removals are done first, regardless of whether you called remove before or after put methods on this editor.

    Parameters key The name of the preference to remove. Returns Returns a reference to the same Editor object, so you can chain put calls together.